package com.itbaizhan;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PipedWriter;
import java.io.PrintWriter;
/*@WebInitParam
属性名	        类型	        作用
name	        String	    param-name
value	        String	    param-value
description	    String	    description*/
/*配置初始化参数*/
/*initParams里面的name与value是init-param标签里面的param-name与param-value的值   注意：用豆号隔开*/
@WebServlet(urlPatterns = "/initParam.do",initParams = {@WebInitParam(name = "key1",value = "ITBZ"),@WebInitParam(name = "key2",value = "XST")},loadOnStartup = 1)
public class InitParamServlet extends HttpServlet {
    @Override
    public void init() throws ServletException {
        //获取ServletConfig
        ServletConfig servletConfig = this.getServletConfig();
        String value1 = servletConfig.getInitParameter("key1");
        String value2 = servletConfig.getInitParameter("key2");
        System.out.println(value1);
        System.out.println(value2);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取servletConfig
        ServletConfig servletConfig = this.getServletConfig();
        String value1 = servletConfig.getInitParameter("key1");
        String value2 = servletConfig.getInitParameter("key2");
        PrintWriter pw = resp.getWriter();
        pw.println(value1);
        pw.println(value2);
        pw.flush();
        pw.close();
    }
}
/*在Servlet3.0以及之后的版本中支持注解式开发Servlet。
对于Servlet的配置不在依赖于web.xml配置文件，
而是使用@WebServlet注解完成Servlet的配置。

属性名	        类型	                作用
initParams	WebInitParam[]	    Servlet的init参数
name	        String	        Servlet的名称
urlPatterns	    String[]	    Servlet的访问URL，支持多个
value	        String[]	    Servlet的访问URL，支持多个
loadOnStartup	int	            自启动Servlet
description	    String	        Servlet的描述
displayName	    String	        Servlet的显示名称
asyncSupported	boolean	        声明Servlet是否支持异步操作模式*/